define(function(require, exports, module) {
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var muiTips = require("muiTips"); //封装的弹框

	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityClient = authorityInit.getJurisdiction(authorityData, sourceType.INFO_CUSTOMER); //客户权限判断
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//钉钉返回
	var dingTalkNav = require('dd_config');

	$(function() {
		//得到客户Id
		var clientId = sessionStorage.getItem("clientId");
		if(authorityClient.indexOf(3) === -1) {
			muiTips.alertTips(' ', '您没有操作客户的权限，请联系管理员开通吧！', 2, 3, "default");
			$('#sub-btn > a').remove();
			dingTalkNav.RightNavBar(false, "删除", "default");
		} else {
			$('#sub-btn').show();
			$("header > a").show();
			dingTalkNav.RightNavBar(true, "删除", getRightFun);
		}

		function getRightFun() {
			var btnArray = ['否', '是'];
			mui.confirm('确认删除？', '提示 ', btnArray, function(e) {
				if(e.index == 1) {
					deleteClientDetails(clientId);
				} else {
					//						info.innerText = 'MUI没有得到你的认可，继续加油'
				}
			});
		}
		//      var concatId = sessionStorage.getItem("clientId");
		getClientDetails(clientId);
		mui("body").on("tap", "#delete", function() {

			var btnArray = ['否', '是'];
			mui.confirm('确认删除？', '提示 ', btnArray, function(e) {
				if(e.index == 1) {
					deleteClientDetails(clientId);
				} else {
					//						info.innerText = 'MUI没有得到你的认可，继续加油'
				}
			});

		});

		function getClientDetails(clientId) {
			//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回
			var option = {};
			var dataModel = {
				id: clientId
			};
			abp.services.app.customer.getModel(dataModel, option).
			done(function(data) {
				//赋值
				$("#companyName").html(data.companyName);
				$("#companySerial").html(data.companySerial);
				$("#catalogName").html(data.infoCompanyCatalog.companyCatalogName);
				$("#amount").html(data.amount);
				$("#contact").prepend(data.infoContactList[0].contactName);
				$("#contactMobile").html(data.infoContactList[0].contactMobile);
				$("#contactTel").html(data.infoContactList[0].contactTel);
				$("#address").html(data.address);
				$("#notes").html(data.notes);
				//存储当前用户的参数
				mui("body").on("tap", "#save-client-data", function() {
					sessionStorage.clientData = JSON.stringify(data);
					//			       		sessionStorage.setItem("title","修改客户");
				});
				if(data.success) {

				} else {
					//console.log(data.error.message)

				}
			}).fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		}

		/*删除客户*/
		function deleteClientDetails(clientId) {
			//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回
			var option = {};
			var dataModel = {
				"id": clientId,
				"companySerial": $("#companySerial").html()
			};
			abp.services.app.customer.delete(dataModel, option).
			done(function(data) {
				clearSession();
				document.location.href = "../../src/chioceClient/chioceClientList.html?dd_nav_bgcolor=FFFF943E";
				if(data.success) {

				} else {}
			}).fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		}

	});
	mui("body").on("tap", "header>button", function() {
		clearSession();
	});

	//ios
	dingTalkNav.LeftNavBar(clearSessionDD);
	//ad
	dingTalkNav.leftAndroid(clearSessionDD);

	function clearSessionDD() {
		sessionStorage.removeItem("chioceClientName");
		sessionStorage.removeItem("chioceClientNameId");
		sessionStorage.removeItem("clientData");
		sessionStorage.removeItem("clientId");
		document.location.href = "../../src/chioceClient/chioceClientList.html?dd_nav_bgcolor=FFFF943E";
	}

	function clearSession() {
		sessionStorage.removeItem("chioceClientName");
		sessionStorage.removeItem("chioceClientNameId");
		sessionStorage.removeItem("clientData");
		sessionStorage.removeItem("clientId");
	}
});